<?php
/**
 * Created by PhpStorm.
 * User: 姚彪
 * Date: 2018/5/28
 * Time: 21:28
 */
namespace app\api\controller\v1;

use app\api\model\ArticleClass;
use think\exception\DbException;
use think\Request;
use \app\api\model\ArticleModel;
class Article{
    /**
     * 添加文章
     * @return \think\response\Json
     */
    public function add(){
        $data = Request::instance()->post();
        $rs = ArticleModel::create($data);
        $returnMessage = array(
            'code' => '200',
            'msg' => '添加成功'
        );
        if ($rs){
            return json($returnMessage);
        }
        $returnMessage['code'] = '500';
        $returnMessage['mes'] = '添加失败';
        return json($returnMessage);
    }

    /**
     * 查询所有分类
     * @return \think\response\Json
     */
    public function queryClass(){
        try {
            return json(ArticleClass::all());
        }catch (DbException $dbException){
            throw new \RuntimeException($dbException->getMessage());
        }
    }

    /**
     * 根据分类查询文章
     * @param int $page
     * @param int $size 每页条数
     * @param int $class_id 分类id
     * @return \think\response\Json
     */
    public function query($size = 10, $class_id){
        $articleModel = new ArticleModel();
        try {
            $rs = $articleModel->where('article_class_id', $class_id)->paginate($size);
            $returnMessage = array(
                'code' => '200',
                'msg' => '查询成功',
                'data' => ''
            );
            if ($rs->isEmpty()){
                $returnMessage['code'] = '500';
                $returnMessage['msg'] = '查询失败';
                return json($returnMessage);
            }
            $returnMessage['data'] = $rs;
            return json($returnMessage);
        }catch (DbException $dbException){
            throw new \RuntimeException($dbException->getMessage());
        }
    }

    /**
     * 查询文章详情
     * @param int $article_id
     * @return \think\response\Json
     */
    public function queryOne($article_id){
        $articleModel = new ArticleModel();
        try{
            $rs = $articleModel->where('article_id',$article_id)->find();
            $returnMessage = array(
                'code' => '200',
                'msg' => '查询成功',
                'data' => ''
            );
            if ($rs){
                $returnMessage['data'] = $rs;
                return json($returnMessage);
            }
            $returnMessage['code'] = '500';
            $returnMessage['msg'] = '查询失败';
            return json($returnMessage);
        }catch (DbException $dbException){
            throw new \RuntimeException($dbException->getMessage());
        }
    }

    /**
     * 删除文章
     * @param $article_id
     * @return \think\response\Json
     */
    public function deleteArticle($article_id){
        $articleModel = new ArticleModel();
        $rs = $articleModel->where($article_id)->delete();
        $returnMessage = array(
            'code' => '200',
            'msg' => '删除成功'
        );
        if ($rs){
            return json($returnMessage);
        }
        $returnMessage['code'] = 500;
        $returnMessage['msg'] = '删除失败';
        return json($returnMessage);
    }
}